﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

using Common;

using Castle.Core.Interceptor;
using Castle.DynamicProxy;

namespace com.globalwave.website
{
    /// <summary>
    /// Board 的摘要说明
    /// </summary>
    public class BoardService : BaseService
    {
        private static NameValueCollection whereMap = null;
        private const string TABLE_NAME = "website_Board";
        private const string TABLE_BULLETIN = "website_Bulletin";
        private const string TABLE_TOPIC = "website_Topic";

        public BoardService()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public BoardService(BaseService other)
            : base(other) 
        {
        }
        virtual public DataSet query(
                NameValueCollection criterion,
                int pageIndex,
                int pageSize,
             string orderString)
        {

            return dao.queryTable(TABLE_NAME, pageIndex, pageSize, criterion, orderString == null ? " order by level_, order_ desc, name_, board_id " : orderString);
        }

        virtual public int create(NameValueCollection board)
        {
            return dao.insertTable(TABLE_NAME, board);
        }

        virtual public int update(NameValueCollection board)
        {
            NameValueCollection criterion = new NameValueCollection();
            string id = board.Get("board_id");

            criterion.Add("board_id", id);

            board.Remove("board_id");

            int result = dao.updateTable(TABLE_NAME, board, criterion);

            board.Add("board_id", id);

            return result;
        }

        virtual public int delete(NameValueCollection criterion)
        {
            if (criterion.Get("board_id") != null)
            {
                throw new BaseException("E000001", "参数错误");
            }
            // delete cascade 
            dao.deleteTable(TABLE_BULLETIN, criterion);
            dao.deleteTable(TABLE_TOPIC, criterion);

            return dao.deleteTable(TABLE_NAME, criterion);

        }

        virtual public void deleteAll(string[] boardIds)
        {
            if (boardIds == null || boardIds.Length == 0)
            {
                throw new BaseException("", "parames");
            }

            NameValueCollection criterion = new NameValueCollection();
            foreach (string boardId in boardIds)
            {
                criterion.Add("board_ids", boardId);
            }

            // delete cascade
            dao.deleteTable(TABLE_BULLETIN, criterion);
            dao.deleteTable(TABLE_TOPIC, criterion);

            dao.deleteTable(TABLE_NAME, criterion);

        }

        /*
        virtual public DataSet getAllByUserId(string user_id)
        {
            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("@user_id", user_id);

            return dao.query(boardCriterion.USER_ALL_board, -1, criterion, null);
        }


        virtual public DataSet possessedByUser(string user_id)
        {
            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("@user_id", user_id);

            return dao.query(boardCriterion.board_PROSESSED_BY_USER, -1, criterion, null);
        }

        virtual public DataSet possessedByOrganization(string organizationId)
        {
            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("@board_id", organizationId);

            return dao.query(boardCriterion.board_PROSESSED_BY_ORGANIZATION, -1, criterion, null);
        }
        */

        public NameValueCollection getWhereConfigMap()
        {
            if (whereMap != null)
            {
                return whereMap;
            }

            whereMap = new NameValueCollection();

            whereMap.Add("board_ids", " board_id in (@board_ids) ");

            return whereMap;
        }

        public static BoardService getInstance()
        {

            ProxyGenerator generator = new ProxyGenerator();

            return generator.CreateClassProxy<BoardService>(new TransactionInterceptor());

        }
    }


    public class boardCriterion
    {

        public const String board_PROSESSED_BY_ORGANIZATION =
                         " select p.board_id as board_id," +
                         "        p.pro_board_id as pro_board_id," +
                         "        p.code_ as code_ ," +
                         "        p.name_ as name_ ," +
                         "        p.url_ as url_ ," +
                         "        p.desc_ as desc_ , " +
                         "        op.organization_id as organization_id " +
                         "   from system_board p left join (select * from system_organization_board where organization_id=@organization_id) op " +
                         "         on p.board_id = op.board_id ";


        public const String board_PROSESSED_BY_USER =
                         " select p.board_id as board_id," +
                         "        p.pro_board_id as pro_board_id," +
                         "        p.code_ as code_ ," +
                         "        p.name_ as name_ ," +
                         "        p.url_ as url_ ," +
                         "        p.desc_ as desc_ , " +
                         "        up.user_id as user_id " +
                         "   from system_board p left join system_user_board up " +
                         "         on p.board_id = up.board_id " +
                         "        and up.user_id=@user_id";


        public const String USER_ALL_board =
                         " select p.board_id as board_id," +
                         "        p.pro_board_id as pro_board_id," +
                         "        p.code_ as code_ ," +
                         "        p.name_ as name_ ," +
                         "        p.url_ as url_ ," +
                         "        p.desc_ as desc_ " +
                         "   from system_user_organization uo , " +
                         "        system_organization o , " +
                         "        system_organization_board op , " +
                         "        system_board p " +
                         "  where uo.user_id = @user_id" +
                         "    and uo.organization_id = o.organization_id" +
                         "    and o.organization_id = op.organization_id" +
                         "    and op.board_id = p.board_id ";
    }
}
